package com.auto.survey.widget.dialog.decimalpickerdialog

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialogFragment
import com.auto.survey.R
import java.text.DecimalFormat

class DecimalPickerDialog(
    private val initialValue: Double = 0.0,
    private val minValue: Double = 0.0,
    private val maxValue: Double = 10.0,
    private val step: Double = 0.1,
    private val decimalDigits: Int = 1,
    private val onConfirm: (Double) -> Unit
) : AppCompatDialogFragment() {

    private lateinit var tvCurrentValue: TextView
    private lateinit var seekBar: SeekBar
    private val decimalFormat by lazy {
        DecimalFormat("#." + "#".repeat(decimalDigits))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.BlueWhiteDialog)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).apply {
            window?.setDimAmount(0.3f) // 背景透明度
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_decimal_picker, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tvCurrentValue = view.findViewById(R.id.tv_current_value)
        seekBar = view.findViewById(R.id.seek_bar)
        val btnCancel = view.findViewById<Button>(R.id.btn_cancel)
        val btnConfirm = view.findViewById<Button>(R.id.btn_confirm)

        // 计算SeekBar的最大值
        val range = maxValue - minValue
        val maxProgress = (range / step).toInt()
        seekBar.max = maxProgress

        // 设置初始进度
        val initialProgress = ((initialValue - minValue) / step).toInt()
        seekBar.progress = initialProgress
        tvCurrentValue.text = decimalFormat.format(initialValue)

        // 监听SeekBar滑动
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (fromUser) {
                    val currentValue = minValue + progress * step
                    tvCurrentValue.text = decimalFormat.format(currentValue)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })

        // 按钮事件
        btnCancel.setOnClickListener { dismiss() }
        btnConfirm.setOnClickListener {
            val selectedValue = minValue + seekBar.progress * step
            onConfirm(selectedValue)
            dismiss()
        }
    }
}